<template>
  <view class="order-detail-cell">
    <view v-if="film">
      <cell name="电影票" :desc="filmPrice" />
      <cell v-if="vipCardPrice" :name="vipCardLabel" :desc="`￥${vipCardPrice}`" />
      <cell v-for="(item, index) in otherPayInfo" :key="index" :detail="item" :name="item.name" :desc="item.value" />
      <cell v-if="film.cardInfo.cardId" :name="cardName" :desc="`-￥${film.cardInfo.money}`" :color="useColor('theme')" />
      <cell v-if="film.coupons.length" name="电影优惠券" :desc="filmCouponDesc" :color="useColor('theme')" />
      <cell v-if="film.balance" name="余额" :desc="`-￥${film.balance}`" :color="useColor('theme')" />
      <cell v-if="film.integral" name="积分" :desc="`-${film.integral}`" :color="useColor('theme')" />
      <cell v-if="film.vipDiscount" name="会员每月首单立减" :desc="`-￥${film.vipDiscount}`" :color="useColor('theme')" />
    </view>
    <view v-if="goods">
      <cell :name="goodsTitle" :desc="goods.price" />
      <cell v-if="goods.couponMoney" name="卖品券" :desc="`-￥${goods.couponMoney}`" />
      <cell v-if="goods.cardInfo && goods.cardInfo.money" :name="cardName" :desc="`-￥${goods.cardInfo.money}`" :color="useColor('theme')" />
      <cell v-if="goods.balance" name="余额" :desc="`-￥${goods.balance}`" :color="useColor('theme')" />
      <cell v-if="goods.integral" name="积分" :desc="`-${goods.integral}`" :color="useColor('theme')" />
    </view>
  </view>
</template>

<script>
import hook from "@/mixins/hook"
import weixin from "@/mixins/weixin"
import cell from "./cell.vue"
import { mapGetters } from "vuex"
export default {
  name: "order-detail-cell",
  components: { cell },
  mixins: [hook, weixin],
  props: {
    preInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {}
  },
  computed: {
    ...mapGetters(["cardName"]),
    film() {
      return this.preInfo?.film || ""
    },
    // 其他优惠信息
    otherPayInfo() {
      const otherPayInfo = this.film ? this.film.otherPayInfo : []
      return otherPayInfo
    },
    // 其他优惠总价
    otherPayTotal() {
      const total = this.arrayCalc(this.otherPayInfo, "v")
      return total
    },
    // vip首单优惠价格
    vipDiscount() {
      return this.preInfo?.film?.vipDiscount || 0
    },
    filmPrice() {
      const price = this.$u.plus(+this.film.filmPrice, +this.otherPayTotal, this.vipDiscount)
      return "￥" + price
    },
    vipCardLabel() {
      return this.preInfo?.film?.checkVipCardV2Info?.vipCard?.rule?.title || ""
    },
    vipCardPrice() {
      return this.preInfo?.film?.checkVipCardV2Info?.vipCard?.rule?.discountPrice || 0
    },
    filmCouponDesc() {
      const coupons = (this.film && this.film.coupons) || []
      const len = coupons.length
      if (!len) {
        return ""
      }
      let desc = ""
      const isDifference = coupons.find((i) => i.couponType === "1")
      if (isDifference) {
        desc = `-${len}张`
      } else {
        const reduceTotal = this.arrayCalc(coupons, "reduce")
        desc = `-¥ ${reduceTotal}`
      }

      return desc
    },

    goods() {
      if (this.preInfo?.goods?.info) {
        return this.preInfo.goods
      }
      return ""
    }
  },
  methods: {
    arrayCalc(array, priceKey, numKey) {
      const len = array.length || 0
      let sum = 0
      for (let i = len; i--; ) {
        const item = array[i]
        const num = numKey ? item[numKey] : 1
        const price = priceKey ? item[priceKey] : item
        const value = this.$u.times(this.isNaNStr(num), this.isNaNStr(price))
        sum = this.$u.plus(sum, value)
      }
      return sum
    },
    isNaNStr(v) {
      v = (v + "").replace(/\\,/g, "")
      v = +v
      if (isNaN(v)) {
        return 0
      }
      return v
    }
  }
}
</script>

<style lang="scss" scoped></style>
